package de.hft_stuttgart.dsa.ctt.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Eduard Tudenhoefner
 */
public class Curriculum {
	private String curriculumId;
	private List<Course> courses = new ArrayList<Course>();
	private int numberOfCourses;

	public Curriculum() {
		super();
	}

	public Curriculum(String curriculumId, List<Course> courses, int numberOfCourses) {
		super();
		this.curriculumId = curriculumId;
		this.courses = courses;
		this.numberOfCourses = numberOfCourses;
	}

	public Curriculum(String curriculumLine, HashMap<String, Course> courseMap) {

		String[] courseSpecification = curriculumLine.split(" ");
		ArrayList<Course> coursesFromLine = new ArrayList<Course>();
		this.curriculumId = courseSpecification[0];
		int courseNr = Integer.parseInt(courseSpecification[1]);
		this.numberOfCourses = courseNr;
		for (int i = 2; i < 2 + courseNr; i++) {
			Course course = courseMap.get(courseSpecification[i]);
			course.getCurriculums().add(this);
			coursesFromLine.add(course);
		}
		this.courses = coursesFromLine;
	}

	public String getCurriculumId() {
		return curriculumId;
	}

	public void setCurriculumId(String curriculumId) {
		this.curriculumId = curriculumId;
	}

	public List<Course> getCourses() {
		return courses;
	}

	private String getCoursesAsString() {
		String courseString = "";
		for (Course c : courses) {
			courseString += c.getCourseId() + ", ";
		}

		return courseString;
	}

	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}

	public int getNumberOfCourses() {
		return numberOfCourses;
	}

	public void setNumberOfCourses(int numberOfCourses) {
		this.numberOfCourses = numberOfCourses;
	}

	@Override
	public String toString() {

		return "curriculumId: " + curriculumId + ", NumberofCourses: " + numberOfCourses + ", Courses: " + getCoursesAsString();
	}

	public boolean containsCourse(Course courseToFind) {
		if (null == courseToFind.getCourseId()) {
			return false;
		}
		for (Course course : getCourses()) {
			if (course.getCourseId().equals(courseToFind.getCourseId())) {
				return true;// Curriculum contains the Course with this CourseId
			}
		}

		return false;

	}

	public boolean containsAtLeastOneOfTheseCourses(Object[] coursesToFind) {
		Course course;
		for (Object courseObject : coursesToFind) {
			course = (Course) courseObject;
			if (null == course.getCourseId()) {
				continue;
			}
			for (Course currentCourse : getCourses()) {
				if (currentCourse.getCourseId().equals(course.getCourseId())) {
					return true;// Curriculum contains the Course with this
								// CourseId
				}
			}
		}
		return false;
	}
}